using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerStatusInformation : MonoBehaviour, IAttack
{
    private PlayerController playerController;
    // 飞行
    public bool IsFly { get; set; }

    public Vector2 flySpeed;
    public Vector2 GetFlySpeed
    {
        get { return flySpeed; }
    }
    // 复活
    public bool IsRevive { get; set; }
    
    // 受伤  死亡 标记
    public bool PlayerIsHit { get; set; }
    private bool playerIsDeath = false;
    public bool GetPlayerIsDeath
    {
        get { return playerIsDeath; }
    }
    [Header("受伤无敌")]
    [SerializeField] private float hitInvincibleTime; // 受伤无敌持续时间

    private float invincibleTimer;                    // 受伤无敌计时器
    private bool isInvincible = false;                // 受伤无敌标记
    public bool SetIsInvincible
    {
        set { isInvincible = value; }
    }
    
    //
    private void Start()
    {
        playerController = GetComponent<PlayerController>();
        invincibleTimer = hitInvincibleTime;
        IsRevive = false;
        // 添加 死亡后，游戏结束 事件     -- 该事件为玩家复活
        EventCenter.Instance.AddEventListener<bool>(EventName.EVENT_GAME_RESTART, (bool value)=>IsRevive = value);
    }
    
    //
    private void Update()
    {
        // 死亡判定，玩家掉落边界直接死亡
        if(this.transform.position.y < -20f)
            TakePlayerDeath();
        // 不是受伤无敌状态，直接返回，不执行下面步骤
        if (!isInvincible)
            return;
        InvincibleAction();
    }
    
    //
    public void Attack()
    {
        // 玩家受伤判断是否处于无敌时间，不是就继续执行
        if(isInvincible)
            return;
        // 玩家血量是否为零，为零标记为死亡
        if (playerController.GetPlayerBloodStatus() == PlayerBloodStatues.LessHP)
            playerIsDeath = true;
        // 不处于无敌并且血量健康则标记为受伤状态，受伤无敌打开
        PlayerIsHit = true;
        isInvincible = true;
        // 扣除血量
        playerController.ReducePlayerBlood();
    }

    private void TakePlayerDeath()
    {
        // 玩家直接死亡
        playerIsDeath = true;
        // UI直接到死亡界面
        playerController.TakePlayerDeath();
    }
    
    /// <summary>
    /// 受伤无敌行为函数
    /// </summary>
    private void InvincibleAction()
    {
        // 处于受伤无敌状态，进行受伤无敌计时
        invincibleTimer -= Time.deltaTime;
        if (invincibleTimer < 0)
        {
            // 计时器结束，状态时间归位
            isInvincible = false;
            invincibleTimer = hitInvincibleTime;
        }
        
        // 受伤无敌人物闪烁
    }
    
    /// <summary>
    /// 状态初始化
    /// 玩家死亡重新开始游戏时调用
    /// 初始化变量为 受伤 死亡 无敌
    /// </summary>
    public void PlayerStateInitialization()
    {
        PlayerIsHit = false;
        playerIsDeath = false;
        isInvincible = false;
    }

    /// <summary>
    /// 触发函数
    /// 触发场景内可交互物体
    /// </summary>
    /// <param name="collidingObj">触碰到的物体</param>
    private void OnTriggerEnter2D(Collider2D collidingObj)
    {
        //
        switch (collidingObj.gameObject.tag)
        {
            // 恢复药品
            case "Restoratives" :
                // print("碰到药瓶");
                if (playerController.GetPlayerBloodStatus() == PlayerBloodStatues.FullHP)
                    return;
                // {
                //     print("血量是满的");
                //     return;
                // } 
                playerController.AddPlayerBlood();
                // print("恢复血量");
                // Destroy(collidingObj.gameObject);
                break;
            // 得分道具
            case "Score" :
                AudioManager.Instance.PlayAudioSourceSFX(AudioSFXPath.PROP_SCORE, this.gameObject);
                IInteraction interaction = collidingObj.GetComponent<IInteraction>();
                playerController.AddPlayerScore(interaction.GetScore());
                // print("加分");
                Destroy(collidingObj.gameObject);
                break;
            case "SaveData" :
                print("存档");
                AudioManager.Instance.PlayAudioSourceSFX(AudioSFXPath.PROP_SAVE, this.gameObject);
                ISaveData save = collidingObj.GetComponent<ISaveData>();
                playerController.PlayerSaveData(save.GetPosition(), save.GetSceneIndex());
                break;
            case "NextLevel" :
                EventCenter.Instance.EventTrigger(EventName.EVENT_SCENE_SCENESWITE, SceneManager.GetActiveScene().buildIndex + 1);
                break;
            case "Fly" :
                IFly fly = collidingObj.gameObject.GetComponent<IFly>();
                flySpeed = fly.GetFlySpeed();
                IsFly = true;
                break;
            default: 
                break;
        }
    }
}
